package com.xuanwei.admin.controller;


import com.xuanwei.core.response.ResultVO;
import com.xuanwei.core.service.IXwProductLabelService;
import com.xuanwei.core.util.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mjy
 * @since 2019-04-25
 */
@RestController
@RequestMapping("/api/product/label")
public class ProductLabelController extends BasicController {

    @Autowired
    private IXwProductLabelService productLabelService;

    @RequestMapping(value = "add.do", method = RequestMethod.GET)
    public ResultVO add(String name, String color) {
        if (!ParamUtil.checkIsString(name)) {
            return ResultVO.failResult("标签名格式错误");
        }

        if (!color.startsWith("#")) {
            return ResultVO.failResult("颜色格式错误,只能以#开头");
        }
        productLabelService.addOrUpdate(null, name, color, getId());
        return ResultVO.successResult("添加成功");
    }

    @RequestMapping(value = "update.do", method = RequestMethod.GET)
    public ResultVO update(Integer id, String name, String color) {
        if (!ParamUtil.checkIsPositiveInteger(id)) {
            return ResultVO.failResult("ID参数格式错误");
        }
        if (!ParamUtil.checkIsString(name)) {
            return ResultVO.failResult("标签名格式错误");
        }
        if (!color.startsWith("#")) {
            return ResultVO.failResult("颜色格式错误,只能以#开头");
        }
        productLabelService.addOrUpdate(id, name, color, getId());
        return ResultVO.successResult("修改成功");
    }

    @RequestMapping(value = "delete.do", method = RequestMethod.GET)
    public ResultVO delete(Integer id) {
        if (!ParamUtil.checkIsPositiveInteger(id)) {
            return ResultVO.failResult("ID参数格式错误");
        }
        productLabelService.getBaseMapper().deleteById(id);
        return ResultVO.successResult("删除成功");

    }

    @RequestMapping(value = "list.do", method = RequestMethod.GET)
    public ResultVO list() {

        return ResultVO.successResult(productLabelService.findList());
    }


}
